package org.jeecg.modules.subject.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.information.entity.CourseKnowledge;
import org.jeecg.modules.information.entity.CourseType;
import org.jeecg.modules.information.mapper.CourseKnowledgeMapper;
import org.jeecg.modules.information.mapper.CourseMapper;
import org.jeecg.modules.information.mapper.CourseTypeMapper;
import org.jeecg.modules.subject.entity.Subject;
import org.jeecg.modules.subject.mapper.SubjectMapper;
import org.jeecg.modules.subject.model.SubjectModel;
import org.jeecg.modules.subject.service.ISubjectService;
import org.jeecg.modules.subject.vo.SubjectVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 题目表
 * @Author: jeecg-boot
 * @Date:   2021-08-15
 * @Version: V1.0
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {
    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private CourseTypeMapper courseTypeMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseKnowledgeMapper courseKnowledgeMapper;

    @Override
    public IPage<SubjectVo> queryPageList(SubjectModel subject, int pageNo, int pageSize) {
        //封装分页对象
        IPage<SubjectVo> iPage = new Page<>(pageNo, pageSize);
        //选择了课程类型，课程，知识点的查询
        List<CourseKnowledge> courseKnowledges=null;//创建知识点对象
        //如果用户只设置了课程类别
        if ((!StringUtils.isEmpty(subject.getCourseTypeId()))
                && StringUtils.isEmpty(subject.getCourseId())
                && StringUtils.isEmpty(subject.getKnowledgeId())) {
            //查询课程id对应的课程对象
            CourseType courseType = courseTypeMapper.selectById(subject.getCourseTypeId());
            //根据课程类别code找到该类别下所有子类别
            QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
            courseTypeQueryWrapper.likeRight("type_code",courseType.getTypeCode());
            List<CourseType> courseTypes = courseTypeMapper.selectList(courseTypeQueryWrapper);
            //根据课程类别集合查找所有类别下的课程id
            List<String> courseIds = courseMapper.selectAllByCourseTypeIds(courseTypes);
            //根据课程id集合再去找课程下的章节知识点集合
            if (courseIds != null && courseIds.size() > 0) {
                QueryWrapper<CourseKnowledge> courseQueryWrapper=new QueryWrapper<>();
                courseQueryWrapper.in("knowledge_course_id", courseIds);
                courseKnowledges = courseKnowledgeMapper.selectList(courseQueryWrapper);
                //按照知识点对象集合拿分页题目集合
                return subjectMapper.selectPageByCourseKnowledges(iPage, subject, courseKnowledges);
            }
            return new Page<SubjectVo>().setRecords(Collections.emptyList());//如果这个类别没有课程，那么代表这个类别没有题目，那么返回一个空的分页对象
        } else if ((!StringUtils.isEmpty(subject.getCourseId())
                && StringUtils.isEmpty(subject.getKnowledgeId()))) {//填写了课程，但是没有填写知识点
            //查询课程的所有知识点
            QueryWrapper<CourseKnowledge> courseKnowledgeQueryWrapper=new QueryWrapper<>();
            courseKnowledgeQueryWrapper.eq("knowledge_course_id", subject.getCourseId());
            courseKnowledges = courseKnowledgeMapper.selectList(courseKnowledgeQueryWrapper);
            if (courseKnowledges == null || courseKnowledges.size() == 0) {//课程没有知识点
                return new Page<SubjectVo>().setRecords(Collections.emptyList());//就直接返回没有数据，否则courseKnowledges为null去查就是相当于没有填写课程类型，课程和知识点地去查
            }
            //按照知识点对象集合拿分页题目集合
            return subjectMapper.selectPageByCourseKnowledges(iPage, subject, courseKnowledges);
        } else if ((!StringUtils.isEmpty(subject.getKnowledgeId()))) {//填写了知识点
            return subjectMapper.selectPageByCourseKnowledge(iPage, subject, subject.getKnowledgeId());
        } else {//没有选择课程类型，课程，知识点。
            return subjectMapper.selectPageByCourseKnowledges(iPage,subject,null);//因为这个方法用了动态sql，没有知识点就不拼接知识点的条件
        }
    }

    @Override
    public void add(Subject subject) {
        //填充创建人，创建时间，更新人，更新时间
        //获得当前登录的用户信息
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        subject.setCreateBy(user.getUsername());
        Date nowTime=new Date();
        subject.setCreateTime(nowTime);
        subject.setUpdateBy(user.getUsername());
        subject.setUpdateTime(nowTime);
        subjectMapper.insert(subject);
    }
}
